/*
 * @(#)DateUtil.java 1.0 2009/02/24
 *
 * Copyright 2009 SuperV System Corporation, Inc. All rights reserved.
 */

package com.lottery.common.utils;

import com.alibaba.druid.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtil {

    // 日期时间格式
    public static final String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_TIME_FORMAT_PATTERN_2 = "yyyy-MM-dd HH:mm";


    // 日期时间格式
    public static final String DATE_TIME_FORMAT_TWIN_PATTERN = "yyyyMMddHHmmss";

    // 日期时间格式
    public static final String DATE_TIME_FORMAT_OTHER_PATTERN = "yyyyMMddHHmm";

    // 日期格式：yyyy-MM-dd
    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";

    public static final String DATE_FORMAT_PATTERN2 = "yyyy/MM/dd";

    // 日期格式：yyyy年MM月dd
    public static final String DATE_FORMAT_CHINESE = "yyyy年MM月dd日";

    // 日期格式：yyyy.MM.dd
    public static final String DATE_FORMAT_PATTERN_TWIN = "yyyy.MM.dd";

    public static Date StringToDateTime(String pDate) {
        try {
            if (StringUtils.isEmpty(pDate)) {
                throw new IllegalArgumentException("illegal date, or is null or empty!");
            }

            Date tmpDate = StringToCalendar(pDate).getTime();
            return tmpDate;
        } catch (Exception e) {
            return null;
        }

    }
    
    public static Date getSystemDate() {
        return Calendar.getInstance().getTime();
    }
    
    public static String getSystemDateTime() {
        return format(getSystemDate(), DATE_TIME_FORMAT_PATTERN);
    }

    private static boolean isValidDate(Date pDate, String pPattern) {

        if (pDate == null || StringUtils.isEmpty(pPattern)) {
            return false;
        }
        return true;
    }

    public static String format(Calendar pCalendar, String pPattern) {

        if (pCalendar == null) {
            return null;
        }
        return format(pCalendar.getTime(), pPattern);
    }

    public static String format(Date pDate, String pPattern) {

        if (!isValidDate(pDate, pPattern)) {
            return null;
        }

        DateFormat dateFormat;
        String formatDateString;
        try {
            dateFormat = new SimpleDateFormat(pPattern);
            dateFormat.setLenient(false);
            formatDateString = dateFormat.format(pDate);
            return formatDateString;
        } catch (Exception e) {
            return null;
        }
//      Calendar calendar = new GregorianCalendar();
//      calendar.setTime(pDate);

//      return format(calendar, pPattern);
    }

    public static String format(Date pDate) {

        return format(pDate, DATE_TIME_FORMAT_PATTERN);
    }

    public static String format2(Date pDate) {

        return format(pDate, DATE_TIME_FORMAT_PATTERN_2);
    }

    public static String formatToDay(Date pDate) {

        return format(pDate, DATE_FORMAT_PATTERN);
    }

    public static String formatToDay2(Date pDate) {

        return format(pDate, DATE_FORMAT_PATTERN2);
    }

     public static String formatDay(Date pDate) {

        return format(pDate, DATE_FORMAT_CHINESE);
    }


    private static Calendar StringToCalendar(String pDate, String pPattern) {
        try {
            if (StringUtils.isEmpty(pDate)) {
                throw new IllegalArgumentException("illegal parameter, or is null or empty!");
            }
            if (StringUtils.isEmpty(pPattern)) {
                pPattern = DATE_TIME_FORMAT_PATTERN;
            }

            Date tmpDate = null;
            DateFormat dateFormat = null;
            dateFormat = new SimpleDateFormat(pPattern);
            
            dateFormat.setLenient(false);
            dateFormat.parse(pDate);
            
            tmpDate = dateFormat.parse(pDate);

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(tmpDate);

            return calendar;
        } catch (Exception e) {
            throw new IllegalArgumentException("illegal parameter, or is null or empty!");
        }
    }

    public static Calendar StringToCalendar(String pDate) {
        return StringToCalendar(pDate, null);
    }

    public static Date StringToDateTimes(String pDate){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
             date = df.parse(pDate);
        } catch (Exception ex) {

        }
        return date;

    }


    public static Date StringToDate(String pDate) {
        return StringToDate(pDate, DATE_FORMAT_PATTERN);
    }

    public static Date StringToDate(String pDate, String pPattern) {
        try {
            if (StringUtils.isEmpty(pDate)) {
                throw new IllegalArgumentException("illegal date, or is null or empty!");
            }
            if (StringUtils.isEmpty(pPattern)) {
                pPattern = DATE_FORMAT_PATTERN;
            }

            Date tmpDate = StringToCalendar(pDate, pPattern).getTime();
            return tmpDate;
        } catch (Exception e) {
            throw new IllegalArgumentException("illegal date parameter");
        }

    }

    public static String format(String leave_start, String pDate) {
        Date date = StringToDateTime(pDate);
        return format(date);
    }

    /**
     * 
     * 验证是否是合法的日期<br>
     * 要求日期格式是：yyyy-MM-dd
     * 
     * @param pDate
     * @return
     */
    public static boolean isValidDate(String pDate) {
        return isValidDate(pDate, null);
    }

    /**
     * 
     * 验证是否是合法的日期<br>
     * 
     * @param pDate 日期字符�?
     * @param pPattern 日期格式模式
     * @return
     */
    public static boolean isValidDate(String pDate, String pPattern) {
        if (StringUtils.isEmpty(pPattern)) {
            pPattern = DATE_FORMAT_PATTERN;
        }
        if (pPattern == null) {
            return false;
        }
        try {
            if (StringUtils.isEmpty(pDate) || pDate.getBytes().length != pPattern.getBytes().length) {
                return false;
            }
            StringToDate(pDate, pPattern);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 
     * 验证是否合法的日期时间格式，采用默认的日期时间格式模�?br>
     * 默认的日期时间格式模式类似：yyyy-MM-dd HH:mm:ss
     * 
     * @param pDate 日期字符�?
     * @return
     */
    public static boolean isValidDateTime(String pDate) {
        return isValidDate(pDate, DATE_TIME_FORMAT_PATTERN);
    }
    
    public static Date relativeDate(Date ad_Date, int ai_Type, int ai_Number) {
		if (ad_Date == null)
			return null;

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(ad_Date);
		calendar.add(ai_Type, ai_Number);

		return calendar.getTime();
	}
}